#include <cstdio>

int main( )
{
    int t, a, b, i, j, f[ 5001 ], m;
    for ( i = 1; i <= 5000; i++ )
        f[ i ] = 0;
    for ( i = 1; i <= 5000; i++ )
        for ( j = i; j <= 5000; j += i )
            f[ j ]++;
    scanf("%d", &t);
    while ( t-- )
    {
        scanf("%d%d", &a, &b);
        m = a;
        for ( i = a + 1; i <= b; i++ )
            if ( f[ i ] > f[ m ] )
                m = i;
        printf("%d\n", m);
    }
    return 0;
}
